package com.woniuxy.tmsdriverassignment.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;

import com.woniuxy.tmsdriverassignment.domain.Contract;
import com.woniuxy.tmsdriverassignment.service.ContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <P> @Author: ideaAdmin
 * <P> TODO: Class
 * <P> TODO:
 * <P> Date: 2022/4/20:19:16
 * <P> Description:
 */

@RestController
@RequestMapping("/contract")
public class ContractController extends BaseController {

    @Autowired
    private ContractService contractService;

    //新增运输合同
    @PostMapping("/addcontract")
    public String addContract(@RequestBody Contract contract){
        contractService.addContract(contract);
        return "新增合同成功";
    }

    //修改新增的运输合同
    @PostMapping("/updatecontract")
    public String updateContract(@RequestBody Contract contract){
        contractService.updateContract(contract);
        return "修改合同成功";
    }

    //删除新增的运输合同
    @DeleteMapping("/deletecontract/{id}")
    public String deleteContract(@PathVariable("id") int id){
        contractService.deleteContract(id);
        return "删除合同成功";
    }

    //查询所有签订但还未运输合同
    @GetMapping("/allcontract")
    public TableDataInfo allContract(){
        startPage();
        List<Contract> list = contractService.allContract();
        return getDataTable(list);
    }

    //查询对应id订单
    @GetMapping("/getcontractbyid/{id}")
    public AjaxResult getContractById(@PathVariable("id") int id){
        Contract contract = contractService.getContractById(id);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put(AjaxResult.CODE_TAG,200);
        ajaxResult.put(AjaxResult.DATA_TAG,contract);
        ajaxResult.put(AjaxResult.MSG_TAG,"订单查询成功");
        return ajaxResult;
//        return AjaxResult.success("查询成功",contract);
    }

    //查询指定签订未运输订单
    @GetMapping ("/onecontract")
    public TableDataInfo getOneContract( Contract contract){
        System.out.println(contract.getId());
        System.out.println(contract.toString());
        System.out.println("ContractController.后端接收到请求---------------------------");
        startPage();
        List<Contract> list = contractService.getOne(contract);
//        System.out.println("contract1 = " + contract1);
        System.out.println(list.get(0).toString());
        System.out.println(list.size());
//        System.out.println(list.get(2).toString());
        return getDataTable(list);
    }
}
